<?php

/*
 * Description of portfolio_model
 * @author Pham Minh Khoi
 * @email: pmkhoi@gmail.com
 * @package:
 */


class Portfolio_model extends CI_Model {

    //put your code here
    function __constructor() {
        parent::__construct();
    }

    function create($category) {

        $sql = "INSERT INTO `gtb_portfolio` " .
                "(`vi_name`, `en_name`, `vi_summary`, `en_summary`, `vi_description`, " .
                "`en_description`, `portfolio_category_id`, `friendly_url`, `position`, " .
                "`date_add`, `date_upd`, `active`, " .
                " `image`) " .
                "VALUES ";

        $sql .= "(" .
                $this->db->escape($category['vi_name']) . ", " .
                $this->db->escape($category['en_name']) . ", " .
                $this->db->escape($category['vi_summary']) . ", " .
                $this->db->escape($category['en_summary']) . ", " .
                "'" . ($category['vi_description']) . "', " .
                "'" . ($category['en_description']) . "', " .
                intval($category['portfolio_category_id']) . ", " .
                $this->db->escape($category['friendly_url']) . ", " .
                intval($category['position']) . ", " .
                $this->db->escape($category['date_add']) . ", " .
                $this->db->escape($category['date_upd']) . ", " .
                intval($category['active']) . ", " .
                $this->db->escape($category['image']) .
                ");";

        $this->db->query($sql);
        //return $this->db->affected_rows();
        return $this->db->insert_id();
    }

    function update($category) {

        $sql = "UPDATE `gtb_portfolio` SET" .
                " vi_name = " . $this->db->escape($category['vi_name']) . ", " .
                " en_name = " . $this->db->escape($category['en_name']) . ", " .
                " vi_summary = " .  $this->db->escape($category['vi_summary']) . ", " .
                " en_summary = " .  $this->db->escape($category['en_summary']) . ", " .
                " vi_description = '" . ($category['vi_description']) . "', " .
                " en_description = '" . ($category['en_description']) . "', " .
                " portfolio_category_id = " . intval($category['portfolio_category_id']) . ", " .
                " friendly_url = " . $this->db->escape($category['friendly_url']) . ", " .
                " position = " . intval($category['position']) . ", " .
                " date_add = " . $this->db->escape($category['date_add']) . ", " .
                " date_upd = " .$this->db->escape($category['date_upd']) . ", " .
                " active = " .intval($category['active']) . 
                " WHERE id = " . intval($category['id']);

        $this->db->query($sql);
        return $this->db->affected_rows();
        //return $this->db->insert_id();
    }

    function getAll($offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE, $para = '') {
        $sql = "SELECT * FROM gtb_portfolio
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);

        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    function countAll($para = '') {
        $sql = "SELECT count(*) as number_record
		FROM gtb_portfolio
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->number_record;
        }
    }

    function getMaxPosition() {
        $sql = "SELECT max(position) as max_position
		FROM gtb_portfolio
		";
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->max_position;
        }
    }

    function findByID($id) {
        $sql = "SELECT *
		FROM gtb_portfolio
                WHERE id = " . intval($id);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }
        
        return null;
    }

    function getAllFrontEnd($offset = 0, $limit = NUMBER_RECORD_PER_PRODUCT_PAGE, $type_id = 0) {
        $sql = "SELECT id as item_id, vi_name as name, vi_description, image as item_image FROM gtb_portfolio
                WHERE active = 1 AND (portfolio_category_id = ".  intval($type_id)." OR 0 = ".  intval($type_id).")
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $query = $this->db->query($sql);
        $result = $query->result_array();

        return $result;
    }

    function getNewest($type_id = 0) {
        $sql = "SELECT *
		FROM gtb_portfolio
                WHERE active = 1 AND (portfolio_category_id = ".  intval($type_id)." OR 0 = ".  intval($type_id).")
                ORDER BY id DESC
                LIMIT 0,1 ";
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    function getByFriendlyUrl($link_rewrite){

		$sql = "SELECT *
				FROM gtb_portfolio
				WHERE friendly_url = " . $this->db->escape($link_rewrite);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function getByCode($code){

		$sql = "SELECT *
				FROM gtb_portfolio
				WHERE code = " . $this->db->escape($code);
		$query = $this->db->query($sql);
		if($result = $query->result()){
			return $result[0];
		}

		return null;
	}

        function setImage($image, $portfolioID){
            $sql = "UPDATE gtb_portfolio SET image = ". $this->db->escape($image).
                   " WHERE id = " . intval($portfolioID);
            $query = $this->db->query($sql);
            return $this->db->affected_rows();
        }

        function findByCategory($cid, $offset = 0, $limit = 10) {
        $sql = "SELECT *
		FROM gtb_portfolio
                WHERE portfolio_category_id IN( " . $cid .") AND (special = 1 OR best_sell = 1)
                ORDER BY position DESC
                LIMIT " . intval($offset) . "," . intval($limit);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result;
        }
    }
}

?>
